/**
 * Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */
package com.mbfasttrack.controller;

import com.mbfasttrack.model.Action;
import com.mbfasttrack.model.Tag;
import com.mbfasttrack.model.User;
import com.mbfasttrack.service.ActionService;
import com.mbfasttrack.service.CommentService;
import com.mbfasttrack.service.GeonamesService;
import com.mbfasttrack.service.GroupService;
import com.mbfasttrack.service.HardwareService;
import com.mbfasttrack.service.LikeService;
import com.mbfasttrack.service.MediaService;
import com.mbfasttrack.service.TagService;
import com.mbfasttrack.service.TrackPointService;
import com.mbfasttrack.service.TrackService;
import com.mbfasttrack.service.UserService;
import java.util.HashMap;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import org.apache.velocity.tools.generic.MathTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
public class Base {

    /**
     *
     */
    public static final Logger log = Logger.getLogger(Base.class);
    /**
     *
     */
    @Autowired
    public ServletContext context;
    /**
     *
     */
    @Resource(name = "MediaService")
    public MediaService mediaService;
    /**
     *
     */
    @Resource(name = "HardwareService")
    public HardwareService hardwareService;
    /**
     *
     */
    @Resource(name = "ActionService")
    public ActionService actionService;
    /**
     *
     */
    @Resource(name = "CommentService")
    public CommentService commentService;
    /**
     *
     */
    @Resource(name = "LikeService")
    public LikeService likeService;
    /**
     *
     */
    @Resource(name = "GeonamesService")
    public GeonamesService geonamesService;
    /**
     *
     */
    @Resource(name = "TagService")
    public TagService tagService;
    /**
     *
     */
    @Resource(name = "TrackService")
    public TrackService trackService;
    /**
     *
     */
    @Resource(name = "UserService")
    public UserService userService;
    /**
     *
     */
    @Resource(name = "GroupService")
    public GroupService groupService;
    /**
     *
     */
    @Resource(name = "TrackPointService")
    public TrackPointService trackPointService;
    /**
     *
     */
    @Value("${sys.site.url}")
    public String sys_site_url;
    /**
     *
     */
    public User user_logged = null;

    public boolean isGuest() {

        if (!SecurityContextHolder.getContext().getAuthentication().isAuthenticated()
                || SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals("anonymousUser")) {
            return true;
        }

        return false;

    }

    /**
     *
     * @param model
     * @return
     */
    public Model getBaseModel(Model model) {
        //tagcloude
        Page<Tag> tagcloude = tagService.tagRepository.findAll(new PageRequest(0, 20, Sort.Direction.DESC, "peso"));
        model.addAttribute("tagcloude", tagcloude);

        HashMap stats = new HashMap();
        stats.put("track_count", trackService.trackRepository.count());
        stats.put("user_count", userService.userRepository.count());
        stats.put("group_count", groupService.groupRepository.count());

        model.addAttribute("stats", stats);


        if (this.user_logged != null) {
            model.addAttribute("user", user_logged);
            model.addAttribute("activity", trackService.getDistinctCountActivityByUserId(user_logged.getId()));
        }

        try {
            Page<Action> globalStream = actionService.actionRepository.findAll(new PageRequest(0, 15, Sort.Direction.DESC, "dateSaved"));
            model.addAttribute("globalstream", globalStream);
        } catch (Exception e) {
            log.fatal(e);
        }

        HashMap util = new HashMap();
        util.put("like", likeService);
        util.put("group", groupService);
        util.put("hardware", hardwareService);
        util.put("math", new MathTool());


        model.addAttribute("util", util);


        return model;
    }
}
